package cn.sinata.wanglin.publish.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.xilada.xldutils.activitys.SelectPhotoDialog;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.DialogUtils;
import com.xilada.xldutils.utils.FileUtils;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.StringUtils;
import com.xilada.xldutils.utils.TimeUtils;
import com.xilada.xldutils.utils.Utils;
import com.xilada.xldutils.view.TwoTextLinearView;
import com.xilada.xldutils.xldUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Lableinfo;
import cn.sinata.wanglin.model.Pubactive;
import cn.sinata.wanglin.neighbor.activity.EditCommentActivity;
import cn.sinata.wanglin.utils.RelativeDateFormat;

/**
 * 发布---发布活动
 */
public class PubactivityActivity extends TitleActivity {

    @BindView(R.id.addimage)
    SimpleDraweeView addimage;//增加图片
    @BindView(R.id.et_content)
    EditText et_content;//活动标题
    @BindView(R.id.ttlv_starttime)
    TwoTextLinearView ttlv_starttime;//开始时间
    @BindView(R.id.ttlv_stoptime)
    TwoTextLinearView ttlv_stoptime;//结束时间
    @BindView(R.id.tv_address)
    TextView tv_address;//地点
    @BindView(R.id.et_money)
    EditText et_money;//报名费用
    @BindView(R.id.ed_peoplenumber)
    EditText ed_peoplenumber;//报名人数
    @BindView(R.id.rg_choicemode)
    RadioGroup rg_choicemode;//选择人数是否限制
    @BindView(R.id.tv_phone)
    EditText tv_phone;
    private String phone=null;//手机号码
    private  String path=null;//图片路径
    private  String startTime=null;//开始时间
    private  String endTime=null;//结束时间
    private  String address=null;//地址
    private  String longitude=null;//经度
    private  String latitude=null;//纬度
    private boolean isPub = false;

    @Override
    protected int setContentId() {
        return R.layout.activity_pubactivity;
    }
    @Override
    protected void initView() {
        super.initView();
        setTitle("发起活动");
        addRightButton("下一步", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nextTo();
            }
        });
        phone= SharedPreferencesUtils.getString(Constant.SP_PHONE);
        if(!TextUtils.isEmpty(phone)){
            tv_phone.setText(phone);
        }

    }

    /**
     * 下一步
     */
    private void nextTo() {
        if(TextUtils.isEmpty(path)){
            showToast("请先选择活动照片！");
            return;
        }
        String content= et_content.getText().toString();
        if(TextUtils.isEmpty(content)){
            showToast("请输入活动标题！");
            return;
        }
        if(TextUtils.isEmpty(startTime)){
            showToast("请选择活动开始时间！");
            return;
        }
        if(TextUtils.isEmpty(endTime)){
            showToast("请选择活动结束时间！");
            return;
        }
        if(TextUtils.isEmpty(address)){
            showToast("请选择活动地址！");
            return;
        }
        String money= et_money.getText().toString();
        if(TextUtils.isEmpty(money)){
            showToast("请先输入报名费用！");
            return;
        }
//
          String peoplenumber="-1";
          switch (rg_choicemode.getCheckedRadioButtonId()){
              case R.id.rb_singlechoice:
                  //不限人数
                  peoplenumber="-1";
                  break;
              case R.id.rb_morechoice:
                  //限制人数
                 String number= ed_peoplenumber.getText().toString().trim();
                  if(TextUtils.isEmpty(number)){
                      showToast("请输入活动限制人数");
                      return;
                  }
                  peoplenumber=number;
                  break;
          }
        phone= tv_phone.getText().toString().toString();
        if(TextUtils.isEmpty(phone)||phone.length() != 11|| !StringUtils.isPhoneNumberValid(phone) ){
            showToast("请输入正确的手机号!");
            return;
        }
        Pubactive mPubactive=new Pubactive(phone,path,startTime,endTime,address,content,money,peoplenumber);
        mPubactive.setLatitude(latitude);
        mPubactive.setLongitude(longitude);
        ActivityUtil.create(mContext).go(AcitivityremarekActivity.class).put("mPubactive",mPubactive)
                .startForResult(1);

    }

    @OnClick({R.id.ttlv_starttime,R.id.ttlv_stoptime,R.id.addimage,R.id.ly_address})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.ttlv_starttime:
                //描述:开始时间
                hintKbOne();
                choiceTime(0);
                break;
            case R.id.ttlv_stoptime:
                //描述:结束时间
                choiceTime(1);
                break;
            case R.id.addimage:
                //描述:添加图片
                ActivityUtil.create(this).go(SelectPhotoDialog.class).startForResult(5);
                break;
            case R.id.ly_address:
                //描述:选择地址
                ActivityUtil.create(this).go(SreachActivity.class).startForResult(6);
                break;

        }
    }
    //此方法，如果显示则隐藏，如果隐藏则显示
    private void hintKbOne() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Utils.systemErr("=resultCode==="+resultCode);
        Utils.systemErr("=requestCode==="+requestCode);
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            switch (requestCode){
                case 5:
                    if (data!=null){
                         path = data.getStringExtra(SelectPhotoDialog.DATA);
                        addimage.setImageURI(Uri.parse("file://" + path));
                    }
                    break;
                case 6:
                    if (data!=null){
                        String name = data.getStringExtra("name");
                        String addressdetils = data.getStringExtra("address");
                         longitude = data.getStringExtra("longitude");
                         latitude  = data.getStringExtra("latitude");
                        address=name+"|"+addressdetils;
                        tv_address.setText(name+"\n"+addressdetils);
                    }
                    break;
                case 1:
                    isPub = true;
                    setResult(RESULT_OK);
                    finish();
                    break;


            }
        }
    }
    /**
     * 选择时间;0开始1结束；
     */
    private void choiceTime(final int type) {
        //起始时间的选择
        TimePickerView pvTime = new TimePickerView(this, TimePickerView.Type.ALL);
        //控制时间范围
        Calendar calendar = Calendar.getInstance();
        pvTime.setRange(calendar.get(Calendar.YEAR), calendar.get(Calendar.YEAR)+10);//要在setTime 之前才有效果哦
        pvTime.setTime(new Date());
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        //时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                if( RelativeDateFormat.isLowerCurrent(date.getTime())){
                    showToast("选择的时间不能低于当前时间！");
                    return;
                }
                if(type==0){
                    startTime=getTime(date);
                    ttlv_starttime.setRightText(startTime);
                }else{
                    endTime=getTime(date);
                    ttlv_stoptime.setRightText(endTime);
                }
            }
        });
        pvTime.show();
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    @Override
    public void finish() {
        if (isPub) {
            dofinish();
        } else {
            DialogUtils.createNoticeDialog(mContext, "请注意", "您确定要放弃该活动的发布么？", "确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dofinish();
                }
            });
        }

    }

    private void dofinish() {
        if (!TextUtils.isEmpty(xldUtils.PICDIR)) {
            FileUtils.deleteFiles(new File(xldUtils.PICDIR));
        }
        super.finish();
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
